package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.VisitorDao;
import cn.sumpu.app.guoguojie.domain.Visitor;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class VisitorDaoImpl extends SqlSessionDaoSupport implements VisitorDao {

	private final static Logger logger = LoggerFactory
			.getLogger(VisitorDaoImpl.class);

	
	@Override
	public void insertVisitor(Visitor visitor) throws AppBusinessException {
		try {
			getSqlSession().insert(
					"cn.sumpu.app.guoguojie.persistence.VisitorMapper.insertVisitor",visitor);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertVisitor database failure.", e);
			}
			throw new AppBusinessException("insertVisitor database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Visitor> visitorMember(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.VisitorMapper.visitorMember",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("visitorMember database failure.", e);
			}
			throw new AppBusinessException("visitorMember database failure.", e);
		}
	}

	@Override
	public int visitorMemberCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.VisitorMapper.visitorMemberCount",map);
			return (Integer) (obj==null?0:obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("visitorMemberCount database failure.", e);
			}
			throw new AppBusinessException("visitorMemberCount database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Visitor> visitorFamily(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.VisitorMapper.visitorFamily",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("visitorFamily database failure.", e);
			}
			throw new AppBusinessException("visitorFamily database failure.", e);
		}
	}
	
	//他看过谁家
	@SuppressWarnings("unchecked")
	@Override
	public List<Visitor> taVisitorFamily(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.VisitorMapper.taVisitorFamily",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("taVisitorFamily database failure.", e);
			}
			throw new AppBusinessException("taVisitorFamily database failure.", e);
		}
	}
	
	//他看过谁家的总量
	@Override
	public int taVisitorFamilyCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.VisitorMapper.taVisitorFamilyCount",map);
			return (Integer) (obj==null?0:obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("taVisitorFamilyCount database failure.", e);
			}
			throw new AppBusinessException("taVisitorFamilyCount database failure.", e);
		}
	}

	@Override
	public int visitorFamilyCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.VisitorMapper.visitorFamilyCount",map);
			return (Integer) (obj==null?0:obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("visitorFamilyCount database failure.", e);
			}
			throw new AppBusinessException("visitorFamilyCount database failure.", e);
		}
	}

	@Override
	public void updateVisitor(Visitor visitor) throws AppBusinessException {
		try {
			getSqlSession().update(
					"cn.sumpu.app.guoguojie.persistence.VisitorMapper.updateVisitor",visitor);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateVisitor database failure.", e);
			}
			throw new AppBusinessException("updateVisitor database failure.", e);
		}
	}

	@Override
	public Visitor getVisitorById(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Visitor) getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.VisitorMapper.getVisitorById",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getVisitorById database failure.", e);
			}
			throw new AppBusinessException("getVisitorById database failure.", e);
		}
	}

	
	

}
